
function set_rtemplate(e)
{
	var setButton = $(e.target);
	var targetBlock = setButton.parents(".rtemplate_item").eq(0);
	var targetId = targetBlock.attr("id").split("_")[1];		// its id is rtemplate_<id>
	
	var title = window.location.pathname.split("/")[2];				// third part on splitting /change/:title/rtemplates at '/'
	var postUrl = '/set/'+title+'/rtemplates'
	var postData = new Object();

	postData["authenticity_token"] = $("*[name='authenticity_token']").eq(0).val();;
	postData["ajax"]="true";
	postData["rtemplate_id"] = targetId;
	
	$.post(
			postUrl,
			postData,
			function(response){
				set_rtemplate_callback(e,response);
			}
	);
}

function set_rtemplate_callback(e, response)
{
	var responseObj = JSON.parse(response);
	if (responseObj["retVal"] != "set"){
		$.jGrowl(responseObj["retVal"]);				// XXX: Change this action to something better
		return;	
	}	
	
	var prev_set_block = $("#rtemplates_block").find(".current").eq(0);
	var new_set_block = $(e.target).parents(".rtemplate_item").eq(0);

	// interchange the  classes on prev and new set blocks
	prev_set_block.removeClass("current");
	new_set_block.addClass("current");
	
	// interchange the tick mark - show it in the new one and hide it from the old one
	new_set_block.find(".tick_mark").removeClass("hidden");		// removeClass("hidden") = show()
	prev_set_block.find(".tick_mark").addClass("hidden");		// addClass("hidden") = hide()
	
	// interchange the set button - show it in the old one, and hide it from the new one
	new_set_block.find(".set_button").addClass("hidden");
	prev_set_block.find(".set_button").removeClass("hidden");
}
